package com.hiyee.article.controller;

import com.hiyee.api.config.RabbitMqConfig;
import com.hiyee.api.controller.user.ArticleControllerApi;
import com.hiyee.grace.result.ResponseJsonResult;
import com.hiyee.utils.RedisOperator;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class ArticleController implements ArticleControllerApi {

    final static Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    RedisOperator redisOperator;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Value("${server.port}")
    private String port;

    @HystrixCommand
    @Override
    public ResponseJsonResult hello() {
        System.out.println("当前微服务端口：" + port);
//        int a = 1/0;
//        try {
//            Thread.sleep(3500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return ResponseJsonResult.success("hello admin");
    }

    public ResponseJsonResult helloFallback() {
        System.out.println("进入熔断方法...");
        return ResponseJsonResult.success("hystrix 熔断");
    }

    public ResponseJsonResult defaultFallback() {
        System.out.println("进入全局熔断方法...");
        return ResponseJsonResult.success("hystrix 全局熔断");
    }

    @GetMapping("/producer")
    public ResponseJsonResult producer() {
        rabbitTemplate.convertAndSend(
                RabbitMqConfig.EXCHANGE_NAME,
                "article.hello",
                "我是生产者发送消息"
        );
        return ResponseJsonResult.success();
    }
}
